﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface.IEarTagReposiotry;
using IntelligentAgriculture.Domain.Interface.StartInterface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace IntelligentAgriculture.Domain.Impl.StartImpl
{
    public class StartRepository : IStartRepository
    {
        /// <summary>
        /// 上下文
        /// </summary>
        private readonly AgricultureDbContext _db;
     
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db">数据库上下文</param>
        public StartRepository(AgricultureDbContext db)
        {
            _db = db;
           
        }

        /// <summary>
        /// 期初表显示
        /// </summary>
        /// <returns>返回期初数据</returns>
        public async Task<List<Start>> GetStartList()
        {
            return _db.Starts.Where(c => c.Isdel == false).ToList();
        }

        /// <summary>
        /// 批量添加期初表
        /// </summary>
        /// <param name="inputs">实体列表</param>
        /// <returns>受影响的行数</returns>  
        public Task<int> AddStartAsync(List<Start> inputs)
        {
            _db.Starts.AddRangeAsync(inputs);

            return _db.SaveChangesAsync(); // 异步保存所有更改到数据库
        }

        /// <summary>
        /// 逻辑删除期初表
        /// </summary>
        /// <param name="startId">主键id</param>
        /// <returns>受影响的行数</returns>
        public Task<int> DeleteStart(int startId)
        {
            var list = _db.Starts.FirstOrDefault(c => c.StartId == startId);
            if (list != null)
            {
                list.Isdel = true;
                _db.Starts.Update(list);
            }
            return _db.SaveChangesAsync();
        }

        /// <summary>
        /// 批量逻辑删除期初表
        /// </summary>
        /// <param name="startIds">id</param>
        /// <returns>返回数据</returns>
        public Task<int> DeleteRangeStart(IEnumerable<int> startIds)
        {
            foreach (int item in startIds)
            {
                var list = _db.Starts.FirstOrDefault(c => c.StartId == item);
                if (list != null)
                {
                    list.Isdel = true; // 标记为逻辑删除
                    _db.Starts.Update(list);
                }
            }
            return _db.SaveChangesAsync();
        }


        /// <summary>
        /// 反填期初表
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>返回数据</returns>
        public async Task<Start> GetByIdAsync(int startId)
        {
            return await _db.Starts.FindAsync(startId);
        }

        /// <summary>
        /// 修改期初表
        /// </summary>
        /// <param name="start">期初实体</param>
        /// <returns>受影响的行数</returns>
        public Task<int> UpdateAsync(Start start)
        {
            _db.Entry(start).State = EntityState.Modified;
            return _db.SaveChangesAsync();
        }

       
    }
}
